//
//  XHDynamicViewModel.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-29.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHDynamicViewModel.h"
#import "XHURLString.h"
#import "XHTableViewCell.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "customtabViewCell.h"

#define LIST @[@"中国网事",@"山西新闻",@"地市热点",@"县区快报",@"厅级资讯"]
@interface XHDynamicViewModel()
{
    BOOL isload;
}
@property (nonatomic ,strong) XHURLString *urlString;
@end

@implementation XHDynamicViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.urlString = [[XHURLString alloc]init];
        self.hasLoad = NO;
       
        self.dataArray = [NSMutableArray array];
        self.titleArray = [NSArray array];
        self.tempdataArray = [NSMutableArray array];
         isload = NO;
        [self pullData1];
     
        
    }
    return self;
}
-(void)pullData1
{
    NSLog(@"pu");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager GET:[self.urlString getColumnsList:[NSNumber numberWithInt:2]] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
             self.titleArray = (NSArray *)responseObject;
             //NSLog(@"%@",(NSArray*)responseObject);
            NSUserDefaults* defa = [NSUserDefaults standardUserDefaults];
         
            
            [defa removeObjectForKey:@"新闻动态栏目"];
            [defa setObject:self.titleArray forKey:@"新闻动态栏目"];
            [defa synchronize];
            for (id item in self.titleArray)
            {
              
                for (id  item1 in item)
                {
                   // NSLog(@" %@ = %@",item1,item[item1]);
                }
            }
             self.hasLoad = YES;
             [self pullData5];
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        self.hasLoad = YES;
        NSLog(@"2222%@",error);
    }];

}


- (void)pullData22//山西
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    NSString* stg = [NSString stringWithFormat:@"http://192.168.11.78:8080/xinhua/m/articleTemp-2-%@.html",COUTURYCODE];
    
    [manager GET:stg parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
           // [self.dataArray addObject:responseObject];
            self.dataArray = responseObject;
            NSUserDefaults* fef = [NSUserDefaults standardUserDefaults];
            [fef removeObjectForKey:@"新闻动态新闻"];
            [fef setObject:self.dataArray forKey:@"新闻动态新闻"];
            [fef synchronize];
            // NSLog(@"%@",responseObject);
            
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        
        self.hasLoad = YES;
    }];
    
}

-(void)canall
{
    [[AFHTTPRequestOperationManager manager].operationQueue cancelAllOperations];
}

- (void)pullData//县区
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
   
    [manager GET:[self.urlString getNewsWithColumnId:[NSNumber numberWithInt:8] wf:COUTURYCODE]
     //复制时替换成 县区code
     parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.dataArray addObject:responseObject];
           // NSLog(@"%@",responseObject);
            [self pullData2];
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        
        self.hasLoad = YES;
    }];

}

- (void)pullData2//地市
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    
    
    [manager GET:[self.urlString getNewsWithColumnId:[NSNumber numberWithInt:7] wf:CITYCODE] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        //复制时替换成 地市 code
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.dataArray addObject:responseObject];
            // NSLog(@"%@",responseObject);
            [self pullData3];
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        
        self.hasLoad = YES;
    }];
    
}
- (void)pullData3//山西
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    
    
    [manager GET:[self.urlString getNewsWithColumnId:[NSNumber numberWithInt:6] wf:@"140000"] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.dataArray addObject:responseObject];
            // NSLog(@"%@",responseObject);
            NSUserDefaults* sdsd = [NSUserDefaults standardUserDefaults];
            [sdsd removeObjectForKey:@"新闻动态新闻"];
            [sdsd setObject:self.dataArray forKey:@"新闻动态新闻"];
            [sdsd synchronize];
            isload = YES;
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        
        self.hasLoad = YES;
    }];
    
}
- (void)pullData5//全国
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    
    
    [manager GET:[self.urlString getNewsWithColumnId:[NSNumber numberWithInt:54] wf:@"100000"] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            [self.dataArray addObject:responseObject];
            // NSLog(@"%@",responseObject);
            [self pullData];
            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        
        self.hasLoad = YES;
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray count];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string  = @"onepeace";
    /*XHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"XHTableViewCell" owner:self options:nil];
        cell= [array objectAtIndex:0];
    }
    
    cell.title.text= [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"title"];
    cell.describe.text= [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"description"];
    NSString *imageString = [NSString stringWithFormat:@"%@%@",self.urlString.ipAndPort,[[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"image"]];
    
    [cell.image setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic"]];
    id hits = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"hits"];
    if ( hits) {
        cell.hits.text = [NSString stringWithFormat:@"%@",hits];
    }*/
    customtabViewCell* cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell)
    {
        cell = [[customtabViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    id tempArra ;
    if (isload)
    {
        tempArra = self.dataArray;
    }
    else
    {
        tempArra = self.tempdataArray;
        NSLog(@"缓存1 ");
    }
    
    if ([tempArra count] > 0 && indexPath.row < [tempArra count] )
    {
        //NSLog(@"class = %@",[[self.dataArray objectAtIndex:indexPath.row] class]);
        id tempar = [tempArra objectAtIndex:indexPath.row];
        if ([tempar count] <= 0)
        {
            return cell;
        }
        
        [cell setdic:[[tempArra objectAtIndex:indexPath.row] objectAtIndex:0]];
        
         //[cell setdic:[[t objectAtIndex:indexPath.row] objectAtIndex:0]];
    }

   /* if ([self.dataArray count] > 0  && indexPath.row < [self.dataArray count])
    {
        NSNumber* tem = [[self.titleArray objectAtIndex:indexPath.row] objectForKey:@"id"];
        for (id item in self.dataArray)
        {
            NSString* t1 = [tem stringValue];
            NSString* t2 = [item[@"categoryid"] stringValue];
            if ([t1 isEqualToString:t2])
            {
                NSLog(@"%@ = %@ ： %@",[[self.titleArray objectAtIndex:indexPath.row] objectForKey:@"name"],t1,t2);
                [cell setdic:[self.dataArray objectAtIndex:indexPath.row]];
            }
        }
       // [cell setdic:[[self.dataArray objectAtIndex:indexPath.row] objectAtIndex:0]];
    }*/
    [cell setLan:[[self.titleArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}
@end
